package com.swak.reactivex.cluster;

import java.io.Serializable;
import java.util.UUID;

import com.swak.Constants;
import com.swak.registry.URL;
import com.swak.utils.StringUtils;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 端系统唯一 ID
 * 
 * @author 618lf
 */
@Getter
@Setter
@Accessors(chain = true)
@EqualsAndHashCode(exclude = { "url" })
@ToString
public class ID implements Serializable {
	private static final long serialVersionUID = 1L;
	private String id;
	private URL url;

	public static ID remoteID(URL url) {
		String id = url.getParameter(Constants.Peer_Id);
		if (StringUtils.isBlank(id)) {
			id = UUID.randomUUID().toString();
			url.addParameter(Constants.Peer_Id, id);
		}
		return new ID().setId(id).setUrl(url);
	}
}